package com.aishua.aishuawallet.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aishua.aishuawallet.Constant;
import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.bean.ResAuthBean;
import com.aishua.aishuawallet.utils.LogUtils;
import com.github.mikephil.charting.components.IMarker;
import com.google.zxing.client.result.ParsedResultType;
import com.mylhyl.zxing.scanner.encode.QREncode;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by aishua on 2018/7/31.
 */

public class ReceivablesActivity extends AppCompatActivity {

    private static final String TAG = "ReceivablesActivity-";
    private Toolbar toolbar;
    private TextView toolbar_title;
    private TextView tv_adress;
    private EditText et_ethcount;
    private ImageView img_code;
    private Button btn_copy;
    private String tokenaddr;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receivables);
        initData();
        initView();
        generateCode();

    }

    private void initData() {

//        tokenaddr = getIntent().getStringExtra("tokenaddr");
        tokenaddr = Constant.ADDRESS_OWN;
         LogUtils.v(TAG+"tokenaddr:"+tokenaddr);
    }


    private void initView() {

        toolbar = findViewById(R.id.toolbar);
        toolbar_title = findViewById(R.id.toolbar_title);
        tv_adress = findViewById(R.id.tv_adress);
        et_ethcount = findViewById(R.id.et_ethcount);
        img_code = findViewById(R.id.img_code);
        btn_copy = findViewById(R.id.btn_copy);
        toolbar.setTitle("");
        tv_adress.setText(Constant.ADDRESS_OWN);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
        toolbar_title.setText(getResources().getString(R.string.collectioncode));
        btn_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ClipboardManager mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("copaddress", tv_adress.getText());
                mClipboardManager.setPrimaryClip(clipData);
                Toast.makeText(ReceivablesActivity.this,getResources().getString(R.string.copysuc),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void generateCode() {


        final String ethcount = et_ethcount.getText().toString().trim();
        Bitmap bitmap = getBitmap(tokenaddr, ethcount);
        img_code.setImageBitmap(bitmap);

        et_ethcount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                LogUtils.v(TAG+"S:"+s);
                Bitmap bitmap = getBitmap(tokenaddr, s.toString());
                img_code.setImageBitmap(bitmap);


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private Bitmap getBitmap(String address, String ethcount) {

        String qrContent =resultCode2Des(address,ethcount);
        return new QREncode.Builder(ReceivablesActivity.this)
//                        .setColor(getResources().getColor(R.color.colorPrimary))//二维码颜色
                .setColors(0xFF0094FF, 0xFFFED545, 0xFF5ACF00, 0xFFFF4081)//二维码彩色
                .setMargin(0)//二维码边框
                //二维码类型
                .setParsedResultType(TextUtils.isEmpty(qrContent) ? ParsedResultType.URI
                        : ParsedResultType.TEXT)
                //二维码内容
                .setContents(TextUtils.isEmpty(qrContent) ? "http://www.aishuapay.com" :
                        qrContent)
                .setSize(250)//二维码等比大小
                .build().encodeAsBitmap();
    }


    private String resultCode2Des(String code, String des) {

        JSONObject json = new JSONObject();
        try {
            json.put("address", code);
            json.put("ethcount", des);
            LogUtils.v(TAG + "resultCode2Des()--result:" + json.toString());
            return json.toString();
        } catch (JSONException e) {

            e.printStackTrace();
        }
        return null;
    }


}
